con

  _xinfreq = 5_000_000
  _clkmode = xtal1 + pll16x

  ENC_A_PIN     = 0
  ENC_B_PIN     = 1

var

  long  a_count                 'must be continuous

obj

 d      :       "Parallax Serial Terminal"
    
pub go

  result := cognew(@Entry,@a_count)

 d.start(115_200)

 repeat
    d.home
    d.str(string("Encoder Count: "))
    d.dec(a_count)
    d.chars(" ", 9)
    waitcnt(cnt + clkfreq/100)
  
  
dat

        org

Entry

              andn      dira,a_pin_mask   'make encoder pins inputs
              andn      dira,b_pin_mask   'make encoder pins inputs
              mov       count,#0
              mov       old_state,#0
              mov       cur_state,#0

Main
              'sample quadrature encoder state
              test      a_pin_mask,ina       wz
     if_nz    or        cur_state,#1<<0
              test      b_pin_mask,ina       wz
     if_nz    or        cur_state,#1<<1

              'figure out the transition
              mov       t0,old_state
              shl       t0,#2
              add       t0,cur_state   
              mov       t1,#table
              add       t1,t0
              movs      add_val,t1
              mov       old_state,cur_state
              mov       cur_state,#0
              
              'add the appropriate value to the counter
add_val       adds      count,0-0

              'write the counter's value up to hub mem
              wrlong    count,par

              'do it again
              jmp       #Main
              

a_pin_mask    long      1<<ENC_A_PIN
b_pin_mask    long      1<<ENC_B_PIN

table         long      0,-1,1,0
              long      1,0,0,-1
              long      -1,0,0,1
              long      0,1,-1,0

count         res       1

cur_state     res       1
old_state     res       1

t0            res       1
t1            res       1
